package com.acanx.log4j2.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.Objects;

/**
  @Author ACANX
  @Date 2018/12/26
  @Time  18:35:45
  Created by ACANX on 2018/12/26.
  User : 用户对象
 */
@Entity
public class User implements Serializable {

    /**
     * 用户ID 唯一
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    // 自增长策略


    @NotEmpty(message = "姓名不能为空")
    @Size(min=3, max=20)
    @Column(nullable = false, unique = true, length = 32)
    private String name;


    @NotEmpty(message = "邮箱不能为空")
    @Size(max=64)
    @Email(message= "邮箱格式不对" )
    @Column(nullable = false)
    private String email;


    @NotEmpty(message = "密码不能为空")
    @Size(max=128)
    @Column(nullable = false, length = 128, unique = true)
    private String password;

    /**
     * description   用户描述
     */
    private String description;// 描述


    /**
     * 无参构造函数
     */
    public User() {
    }


    /**
     * 全参数构造函数
     * @param id   long 编号
     * @param name  String 名字
     * @param email  String 邮件
     * @param password String 密码
     * @param description   用户描述
     */
    public User(long id, String name, String email, String password, String description) {
        this.id = id;
        this.name = name;
        this.email = email;
        this.password = password;
        this.description = description;
    }

    /**
     * 获取用户ID
     * @return
     */
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }


    /**
     * 自动生成的equals方法
     * @param o
     * @return
     */
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof User)) return false;
        User user = (User) o;
        return getId() == user.getId() &&
                getName().equals(user.getName()) &&
                getEmail().equals(user.getEmail()) &&
                getPassword().equals(user.getPassword()) &&
                getDescription().equals(user.getDescription());
    }

    /**
     * 自动生成的hashCode方法
     * @return
     */
    @Override
    public int hashCode() {
        return Objects.hash(getId(), getName(), getEmail(), getPassword(), getDescription());
    }


    /**
     * 自动生成的toString方法
     * @return String string字符串
     */
    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("User{");
        sb.append("id=").append(id);
        sb.append(", name='").append(name).append('\'');
        sb.append(", email='").append(email).append('\'');
        sb.append(", password='").append(password).append('\'');
        sb.append(", description='").append(description).append('\'');
        sb.append('}');
        return sb.toString();
    }


    /**
     * 自定义的生成Json字符串方法
     * @return  String Json字符串
     */
    public String toJson() {
        final StringBuffer sb = new StringBuffer("UserJson{");
        sb.append("id1=").append(id);
        sb.append(", name1='").append(name).append('\'');
        sb.append(", email1='").append(email).append('\'');
        sb.append(", password1='").append(password).append('\'');
        sb.append(", description1='").append(description).append('\'');
        sb.append('}');
        return sb.toString();
    }
}
